RegisterNetEvent("jobgarger:givecar")
AddEventHandler("jobgarger:givecar", function(vehicle, coords, plate, livery, extra1, extra2)
	local spawnCoords = vector3(coords.x, coords.y, coords.z + 1)
	ESX.Game.SpawnVehicle(vehicle, spawnCoords, 100.0, function(vehicleObj)
		SetVehicleNumberPlateText(vehicleObj, plate)
		SetPedIntoVehicle(PlayerPedId(), vehicleObj, -1)
		SetVehicleLivery(vehicleObj, livery)
		SetVehicleModKit(vehicleObj, 0)
		exports.mono_garage:ClientInventoryKeys("add", plate)
		for k, v in ipairs(extra1) do
			SetVehicleExtra(vehicleObj, tonumber(v), 0)
		end
		for k, v in ipairs(extra2) do
			SetVehicleExtra(vehicleObj, tonumber(v), 1)
		end
	end)
end)
lib.registerMenu(
	{
		id = "lspd",
		title = "LSPD",
		position = "top-right",
		options = {
			{
				label = "中央巡逻分局",
				values = {
					"侦察兵 10",
					"侦察兵MK2 20",
					"托伦斯 30",
					"史塔尼亚 40",
					"侦察兵 01",
					"侦察兵 11",
					"侦察兵MK2 29",
					"侦察兵MK2 1L35",
					"托伦斯 41",
					"托伦斯 55",
					"史塔尼亚 61",
					"史塔尼亚 63",
					"侦察兵 1L71",
					"托伦斯 85",
				},
				defaultIndex = 1,
			},
			{
				label = "中央交通分局",
				values = {
					"1L/A21",
					"1L/A22",
					"1L/A23",
					"1L/A24",
					"24L/A21",
					"24L/A22",
					"24L/A23",
					"14L/A21",
					"14L/A22",
					"14L/A23",
					"1G21",
					"1G22",
					"1G23",
					"1W25",
					"1W26",
				},
				defaultIndex = 1,
			},
			{
				label = "中央警探分局",
				values = {
					"1L/A31",
					"1L/A32",
					"1L/A33",
					"1L/A34",
					"24L/A31",
					"24L/A32",
					"24L/A33",
					"14L/A31",
					"14L/A32",
					"14L/A33",
					"1W35",
					"1W36",
				},
				defaultIndex = 1,
			},
		},
	},
	function(selected, scrollIndex, args)
		if selected == 1 and scrollIndex == 1 then
			local vehicle = "pscout"
			local plate = "91102001"
			local livery = 0
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3, 5 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 2 then
			local vehicle = "psoucenew"
			local plate = "11001010"
			local livery = 0
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 3 then
			local vehicle = "police3"
			local plate = "11001011"
			local livery = 0
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 4 then
			local vehicle = "police"
			local plate = "11001012"
			local livery = 0
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 5 then
			local vehicle = "pscout"
			local plate = "11001013"
			local livery = 1
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3, 5 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 6 then
			local vehicle = "pscout"
			local plate = "11001014"
			local livery = 2
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3, 5 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 7 then
			local vehicle = "pscoutnew"
			local plate = "11001015"
			local livery = 1
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 8 then
			local vehicle = "pscoutnew"
			local plate = "11001016"
			local livery = 2
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 9 then
			local vehicle = "police3"
			local plate = "11001017"
			local livery = 1
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 10 then
			local vehicle = "police3"
			local plate = "11001018"
			local livery = 2
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 11 then
			local vehicle = "police"
			local plate = "11001019"
			local livery = 1
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 12 then
			local vehicle = "police"
			local plate = "11001020"
			local livery = 2
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 13 then
			local vehicle = "pscout"
			local plate = "11001021"
			local livery = 3
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 4, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = { 3, 5 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 14 then
			local vehicle = "police3"
			local plate = "11001022"
			local livery = 3
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 6, 7, 8, 10, 11, 12 }
			local extra2 = { 5, 9 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
	end
)
lib.registerMenu(
	{
		id = "lsfd",
		title = "LSFD",
		position = "top-right",
		options = {
			{ label = "救护车", values = { "RA7", "RA27", "RA47", "RA247", "RA477" }, defaultIndex = 1 },
			{ label = "泵车", values = { "E7", "E477" }, defaultIndex = 1 },
		},
	},
	function(selected, scrollIndex, args)
		if selected == 1 and scrollIndex == 1 then
			local vehicle = "lsfd3"
			local plate = "91101010"
			local livery = 0
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 2 then
			local vehicle = "lsfd3"
			local plate = "91101011"
			local livery = 1
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 3 then
			local vehicle = "lsfd3"
			local plate = "91101012"
			local livery = 2
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 4 then
			local vehicle = "lsfd3"
			local plate = "91101013"
			local livery = 3
			local coords = vector3(440.64, -1021.8, 28.27)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 5 then
			local vehicle = "lsfd3"
			local plate = "91101014"
			local livery = 4
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 1 then
			local vehicle = "fireeng"
			local plate = "91101015"
			local livery = 0
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 2 then
			local vehicle = "fireeng"
			local plate = "91101016"
			local livery = 1
			local coords = vector3(1193.27, -1472.82, 34.69)
			local extra1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
			local extra2 = {}
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
	end
)
lib.registerMenu(
	{
		id = "lsgov",
		title = "LSGOV",
		position = "top-right",
		options = {
			{ label = "拖车", values = { "拖车1", "拖车2", "大拖车3", "小拖车4" }, defaultIndex = 1 },
			{
				label = "巡逻车",
				values = { "驻守华盛顿", "史坦尼亚", "卡林", "半吊子", "海致", "鸽子" },
				defaultIndex = 1,
			},
			{
				label = "运输车辆",
				values = { "处理皮卡", "运输皮卡", "信号牌", "竞速特快", "竞速特快2" },
				defaultIndex = 1,
			},
			{ label = "公务车", values = { "公务1", "公务2", "公务3" }, defaultIndex = 1 },
			{ label = "动物控制", values = { "AC1", "AC2" }, defaultIndex = 1 },
		},
	},
	function(selected, scrollIndex, args)
		if selected == 1 and scrollIndex == 1 then
			local vehicle = "flatbed3"
			local plate = "81101030"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 2 then
			local vehicle = "flatbed3"
			local plate = "81101031"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 3 then
			local vehicle = "brickader"
			local plate = "81101032"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 1 and scrollIndex == 4 then
			local vehicle = "polsadrt"
			local plate = "81101033"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 1 then
			local vehicle = "volwash"
			local plate = "81103010"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 2 then
			local vehicle = "volstan"
			local plate = "81103011"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 3 then
			local vehicle = "lspdpark"
			local plate = "81103012"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 4 then
			local vehicle = "lspdpark2"
			local plate = "81103013"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 5 then
			local vehicle = "lspdpark3"
			local plate = "81103013"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 2 and scrollIndex == 6 then
			local vehicle = "pigeonp"
			local plate = "81103014"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 3 and scrollIndex == 1 then
			local vehicle = "stpickup"
			local plate = "81102020"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 3 and scrollIndex == 2 then
			local vehicle = "stbison"
			local plate = "81102021"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 3 and scrollIndex == 3 then
			local vehicle = "message"
			local plate = "81102022"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 3 and scrollIndex == 4 then
			local vehicle = "stnspeedo"
			local plate = "81102023"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 3 and scrollIndex == 5 then
			local vehicle = "stnspeedo"
			local plate = "81102024"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 4 and scrollIndex == 1 then
			local vehicle = "lspdyanktondp"
			local plate = "81105001"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 4 and scrollIndex == 2 then
			local vehicle = "lspdmeritdp05m"
			local plate = "81105002"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 1 }
			local extra2 = { 2 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 4 and scrollIndex == 3 then
			local vehicle = "lspdmeritdp05m"
			local plate = "81105003"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 5 and scrollIndex == 1 then
			local vehicle = "kennel"
			local plate = "81105AC1"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 2 }
			local extra2 = { 1 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
		if selected == 5 and scrollIndex == 2 then
			local vehicle = "kennel"
			local plate = "81105AC2"
			local livery = 0
			local coords = vector3(-487.72, -199.32, 36.98)
			local extra1 = { 10, 1 }
			local extra2 = { 2 }
			checkcar(vehicle, coords, plate, livery, extra1, extra2)
		end
	end
)
ESX = exports["es_extended"]:getSharedObject()
RegisterNetEvent("esx:playerLoaded")
AddEventHandler("esx:playerLoaded", function(xPlayer)
	ESX.PlayerData = xPlayer
	ESX.PlayerLoaded = true
end)
Citizen.CreateThread(function()
	while true do
		playerPed = GetPlayerPed(-1)
		playerPos = GetEntityCoords(playerPed)
		for i, point in ipairs(config.points) do
			local range = point.range or 5
			if
				GetDistanceBetweenCoords(playerPos.x, playerPos.y, playerPos.z, point.x, point.y, point.z, true) < range
			then
				DrawText("Press E to interact with the item", point.x, point.y, point.z + 2.0, 1.0, 1.0)
				if IsControlJustPressed(0, 38) then
					if ESX.PlayerData.job.name == "police" then
						lib.showMenu("lspd")
					elseif ESX.PlayerData.job.name == "ambulance" then
						lib.showMenu("lsfd")
					elseif ESX.PlayerData.job.name == "gov" then
						lib.showMenu("lsgov")
					end
				end
			end
		end
		Citizen.Wait(0)
	end
end)
RegisterCommand("delcar", function()
	local playerPed = PlayerPedId()
	local vehicle = IsPedInAnyVehicle(PlayerPedId(), false)
	if not vehicle then
		ESX.ShowNotification("你没有坐在车里！")
		return
	end
	local vehicle2 = GetVehiclePedIsIn(playerPed, false)
	local plate = GetVehicleNumberPlateText(GetVehiclePedIsIn(playerPed))
	TriggerServerEvent("jobgarger:deletecar", plate)
	exports.mono_garage:ClientInventoryKeys("remove", plate)
	ESX.Game.DeleteVehicle(vehicle2)
	ESX.ShowNotification("你已成功删除车辆")
	print(plate)
end)
function checkcar(vehicle, coords, plate, livery, extra1, extra2)
	local vehicleInfo =
		{ model = vehicle, coords = coords, plate = plate, livery = livery, extra3 = extra1, extra4 = extra2 }
	TriggerServerEvent("jobgarger:checkcar", vehicleInfo)
end
